package com.pactera.madp.cp.mapper.wm;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pactera.madp.cp.api.entity.wm.WorkMsgEntity;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: yangpengwei
 * @time: 2020/11/17 12:57 下午
 * @description 会话内容存储管理
 */
@Component
public interface WorkMsgMapper extends BaseMapper<WorkMsgEntity> {

    /**
     * 获取最新的 Seq
     *
     * @param corpId 企业 id
     * @return seq
     */
    Integer selectLastSeq(@Param("corpId") long corpId);

    /**
     * 保存数据
     * @param list 数据集合
     */
    boolean insertByMap(@Param("tableName") String tableName, @Param("list") List<WorkMsgEntity> list);

    /**
     * 查询与会话对象相关的聊天记录信息
     */
    WorkMsgEntity selectToUserLastMsg(Map<String, Object> map);

    /**
     * 查询与会话对象相关的聊天记录信息
     * @return
     */
    List<WorkMsgEntity> selectToUserMsg(Map<String, Object> map);

    /**
     * 未交流客户聊天记录查看
     *
     * @param map
     * @return
     */
    List<WorkMsgEntity> selectContactToUserMsg(Map<String, Object> map);

    /**
     * 未交流客户聊天记录统计
     *
     * @param map
     * @return
     */
    Integer selectContactToUserMsgCount(Map<String, Object> map);

    Integer selectToUserMsgCount(Map<String, Object> map);

    /**
     * 统计用户未交流客户数
     *
     * @param map
     * @return
     */
    Integer selectContactCount(Map<String, Object> map);

    WorkMsgEntity countTime(@Param("from")String from,@Param("wxExternalUserid")String wxExternalUserid,@Param("corpId")Integer corpId);

    List<WorkMsgEntity> selectMsgByCorpId(@Param("corpId") String corpId);

    List<WorkMsgEntity> selectMsgByCorpIdByTriggerTime(@Param("corpId") String corpId,@Param("triggerTime") long triggerTime);

    WorkMsgEntity selectByMsgId(@Param("corpId") Integer corpId,@Param("msgId") String msgId);

    List<WorkMsgEntity> getWorkMessagesRangeByCorpIdByDesc(@Param("from") String from,@Param("tolist") String tolist,@Param("corpId") String corpId,@Param("id") Long id,@Param("startTime") long startTime,@Param("endTime") long endTime);

    List<WorkMsgEntity> getWorkMessagesRangeByCorpIdByAsc(@Param("from") String from,@Param("tolist") String tolist,@Param("corpId") String corpId,@Param("id") Long id,@Param("startTime") long startTime,@Param("endTime") long endTime);

    List<WorkMsgEntity> getWorkMessagesRangeByCorpIdWxRoomIdByDesc(@Param("wxRoomId") String wxRoomId,@Param("corpId") String corpId,@Param("id") Long id,@Param("startTime") long startTime,@Param("endTime") long endTime);

    List<WorkMsgEntity> getWorkMessagesRangeByCorpIdWxRoomIdByAsc(@Param("wxRoomId") String wxRoomId,@Param("corpId") String corpId,@Param("id") Long id,@Param("startTime") long startTime,@Param("endTime") long endTime);

    /**
     * 查看当前选定用户的联系人或群聊
     *
     * @param map 查询条件
     * @return
     */

    List<String> selectContactByUser(Map<String, Object> map);

    String getLatest(@Param("wxExternalUserid") String wxExternalUserid, @Param("list") ArrayList<Object> list);

    List<WorkMsgEntity> isTouchqt(@Param("roomWxId") String roomWxId, @Param("unix") long compareUnix);

    /**
     * 全局检索-员工
     *
     * @param map
     * @return
     */
    List<WorkMsgEntity> findUser(Map<String, Object> map);

    /**
     * 全局检索-客户
     *
     * @param map
     * @return
     */
    List<WorkMsgEntity> findContact(Map<String, Object> map);

    /**
     * 全局检索-客户+员工
     *
     * @param map
     * @return
     */
    List<WorkMsgEntity> findUserAndContact(Map<String, Object> map);

    /**
     * 所有记录-
     *
     * @param map
     * @return
     */
    List<WorkMsgEntity> findAll(Map<String, Object> map);

    Integer getMsgNum(long l);

    Integer isTouchOver(@Param("msgIds") List<Integer> msgIds, @Param("begin") Long begin);

    List<WorkMsgEntity> page(@Param("wxRoomId") String wxRoomId, @Param(value = "type") Integer type);

    String getLatestTime(String contactId, String employeeId);

    List<String> getContactWXUserId(Map<String, Object> map);

    List<String> getContactTags(@Param("wxUserId") String wxUserId);

    Integer contactEffective(
            @Param("startTime") long startTime,
            @Param("endTime") long endTime,
            @Param("wxUserId") String wxUserId
    );

    Long findLastSeq(@Param("msgTableName") String msgTableName);

}
